package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacOrgRoleDao;
import org.scboy.rbac.dao.RbacResourceRoleTypeDao;
import org.scboy.rbac.dao.RbacRoleTypeDao;
import org.scboy.rbac.service.RoleTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class RoleTypeServiceImp implements RoleTypeService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model params) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		params.orderBy("sortIndex");
		return dao.page(params);
	}

	@Override
	public List<Model> list(String orgId) {
		Model params=Model.create();
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		params.add("orgId", orgId);
		return dao.queryList(params);
	}

	@Override
	public boolean add(Model model) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		Model old=get(model.getString("code"));
		if(old!=null)return false;
		
		Model maxModel=dao.maxSortIndex();
		Integer sortIndex =0;
		if (maxModel != null && maxModel.getInt("sortIndex")!=null) {
			sortIndex=maxModel.getInt("sortIndex")+1;
		}
		model.add("sortIndex", sortIndex);
		model.add("addTime", new Date());
		model.add("editTime", new Date());
		long count=dao.insert(model);
		return count>0?true:false;
	}

	@Override
	public boolean update(Model model) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		model.add("editTime", new Date());
		long count=dao.update(model,Model.create().add("code", model.getString("code")));
		return count>0?true:false;
	}

	@Override
	@Transactional
	public boolean delete(String code) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		RbacOrgRoleDao orDao=factory.getDao(RbacOrgRoleDao.class);
		RbacResourceRoleTypeDao rrt=factory.getDao(RbacResourceRoleTypeDao.class);
		//角色类型已经挂钩组织，不能删除
		long count=orDao.count(Model.create().add("code", code));
		if(count>0) {
			return false;
		} else {
			rrt.delete(Model.create().add("code", code));
			long result=dao.delete(Model.create().add("code", code));
			return result>0?true:false;
		}
	}

	@Override
	public Model get(String code) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		return dao.get(Model.create().add("code", code));
	}

	@Override
	public boolean sort(String code, int type) {
		RbacRoleTypeDao dao=factory.getDao(RbacRoleTypeDao.class);
		Model model=dao.get(Model.create().add("code", code));
		Model lastModel=null;
		String sortKey="sortIndex";
		String idKey="code";
		//降序
		if(type==0) {
			//查询下面一个系统
			lastModel=dao.get(Model.create().GT(sortKey, model.getInt(sortKey)).orderBy(sortKey));
			
		}else {
			//升序
			lastModel=dao.get(Model.create().LT(sortKey, model.getInt(sortKey)).orderBy(sortKey,OrderType.DESC));
			
		}
		if(lastModel!=null) {
			dao.update(Model.create().add(sortKey, lastModel.getInt(sortKey)),Model.create().add(idKey, code));
			dao.update(Model.create().add(sortKey, model.getInt(sortKey)),Model.create().add(idKey, lastModel.getString(idKey)));
		}
		return true;
	}

}
